package com.haidong.myfile;

import java.util.List;

/**
 * 用户类,存储用户的基本信息,存在用户分组
 */
public class User {
    private String userName;
    private Group group;
    private String password;

    public User(String userName, String password, Group group) {
        this.userName = userName;
        this.password = password;
        this.group = group;
        //在创建用户分组属性的同时,把对应分组对象的用户属性加上
        group.getUsers().add(this);
    }
    public User(String userName, String password){
        this.userName = userName;
        this.password = password;
    }

    //用户的密码是敏感资源,只能在内部校验
    public Boolean judge(String name,String password){
        return name.equals(this.userName) && password.equals(this.password);
    }

    //列出目录功能由用户调用
    public String dir(MyFile now){
        String detail = "";
        List<MyFile> sons = now.getSons();
        for (MyFile son : sons) {
            detail += son.showDetail();
        }
        return detail;
    }

    //创建文件,自动继承在当前目录下
    public MyFile create(String fileName,MyFile now){
        return new MyFile(fileName,now,this);
    }

    //删除文件
    public void delete(String fileName, MyFile now){
        List<MyFile> sons = now.getSons();
        //注意删除元素不要用增强for,增强for用的是迭代器,删除元素会卡死
        for (int i = 0; i < sons.size(); i++) {
            //由于这里我们只要删一个就好,不是去重,所以不用管指针偏移
            if(fileName.equals(sons.get(i).getName())){
                sons.remove(sons.get(i));
            }
        }
    }

    //获取用户所在分组,用来判断用户的访问权限
    public Group getGroup() {
        return group;
    }
}
